package com.blunka.framework.client.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultMutableTreeNode;

import com.blunka.framework.client.constants.DesignConstant;
import com.blunka.framework.client.constants.ImageConstant;
import com.blunka.framework.client.enums.ModelType;
import com.blunka.framework.client.ui.dialog.HelpDialog;
import com.blunka.framework.client.ui.entity.BlkTableMetaData;
import com.blunka.framework.client.ui.tools.ActionUtils;
import com.blunka.framework.client.utils.DesignUtils;

/**
 * 系统工具栏
 * @author xuqiang
 *
 */
public class ToolBarMenu extends JToolBar{


	/**
	 * 工具栏按钮信息
	 */
	private static Map<String,JButton> btnMap = new HashMap<String,JButton>();

	private static ToolBarMenu toolBar = new ToolBarMenu();

	public static ToolBarMenu newInstance(){
		return toolBar;
	}

	private ToolBarMenu(){
		this.add(createSystem());
		this.add(createService());
		this.add(createTable());
		this.add(createView());
		this.add(createGroup());
		this.add(createAttr());
		this.add(createSepator());

		this.add(createEditBtn());
		this.add(createCopyBtn());
		this.add(createPasteBtn());
		this.add(createDelBtn());
		this.add(createSepator());

		this.add(createPreBtn());
		this.add(createNextBtn());
		this.add(createSepator());

		this.add(createSubmitBtn());
		this.add(createSetBtn());
		this.add(createSepator());

		this.add(createHelpBtn());

	}

	/**
	 * 创建分割线
	 * @return
	 */
	private JSeparator createSepator(){
		JSeparator sep = new JSeparator(SwingConstants.VERTICAL);
		sep.setPreferredSize(new Dimension(20,20));
		sep.setMaximumSize(new Dimension(20,20));
		sep.setMinimumSize(new Dimension(20,20));
		return sep;
	}

	/**
	 * 新建系统
	 * @return
	 */
	private JButton createSystem(){
		JButton btn = new JButton("新建系统");
		btn.setIcon(new ImageIcon(ImageConstant.SYSTEM_IMG));
		btn.setToolTipText("新建系统");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.createBlkModel(btn.getText(), ModelType.SYSTEM);
			}
		});
		btnMap.put("SYSTEM", btn);
		return btn;
	}

	/**
	 * 新建业务
	 * @return
	 */
	private JButton createService(){
		JButton btn = new JButton("新建业务");
		btn.setIcon(new ImageIcon(ImageConstant.SERVICE_IMG));
		btn.setToolTipText("新建业务");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.createBlkModel(btn.getText(), ModelType.SERVICE);
			}
		});
		btnMap.put("SERVICE", btn);
		return btn;
	}

	/**
	 * 新建表
	 * @return
	 */
	private JButton createTable(){
		JButton btn = new JButton("新建表");
		btn.setIcon(new ImageIcon(ImageConstant.TABLE_IMG));
		btn.setToolTipText("新建表");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.createTableOrView(btn.getText(), ModelType.TABLE);
			}
		});
		btnMap.put("TABLE", btn);
		return btn;
	}

	/**
	 * 新建试图
	 * @return
	 */
	private JButton createView(){
		JButton btn = new JButton("新建试图");
		btn.setIcon(new ImageIcon(ImageConstant.VIEW));
		btn.setToolTipText("新建试图");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.createTableOrView(btn.getText(), ModelType.VIEW);
			}
		});
		btnMap.put("VIEW", btn);
		return btn;
	}

	/**
	 * 新建组
	 * @return
	 */
	private JButton createGroup(){
		JButton btn = new JButton("新建组");
		btn.setIcon(new ImageIcon(ImageConstant.GROUPS_IMG));
		btn.setToolTipText("新建组");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.createBlkModel(btn.getText(), ModelType.GROUPS);
			}
		});
		btnMap.put("GROUPS", btn);
		return btn;
	}

	/**
	 * 新建属性
	 * @return
	 */
	private JButton createAttr(){
		JButton btn = new JButton("新建属性");
		btn.setIcon(new ImageIcon(ImageConstant.FIELD_IMG));
		btn.setToolTipText("新建属性");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.createField(btn.getText(), ModelType.ATTRIBUTE);
			}
		});
		btnMap.put("ATTR", btn);
		return btn;
	}


	/**
	 * 编辑
	 * @return
	 */
	private JButton createEditBtn(){
		JButton btn = new JButton("编辑");
		btn.setIcon(new ImageIcon(ImageConstant.EDIT));
		btn.setToolTipText("编辑");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}

			}
		});
		btnMap.put("EDIT", btn);
		return btn;
	}

	/**
	 * 复制
	 * @return
	 */
	private JButton createCopyBtn(){
		JButton btn = new JButton("复制");
		btn.setIcon(new ImageIcon(ImageConstant.COPY));
		btn.setToolTipText("复制");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}

			}
		});
		btnMap.put("COPY", btn);
		return btn;
	}

	/**
	 * 粘贴
	 * @return
	 */
	private JButton createPasteBtn(){
		JButton btn = new JButton("粘贴");
		btn.setIcon(new ImageIcon(ImageConstant.PASTE));
		btn.setToolTipText("粘贴");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}

			}
		});
		btnMap.put("PASTE", btn);
		return btn;
	}

	/**
	 * 删除
	 * @return
	 */
	private JButton createDelBtn(){
		JButton btn = new JButton("删除");
		btn.setIcon(new ImageIcon(ImageConstant.DELETE));
		btn.setToolTipText("删除");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}

			}
		});
		btnMap.put("DELETE", btn);
		return btn;
	}

	/**
	 * 上移按钮
	 * @return
	 */
	private JButton createPreBtn(){
		JButton btn = new JButton("上移");
		btn.setIcon(new ImageIcon(ImageConstant.UP));
		btn.setToolTipText("上移");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.MoveNode();
			}
		});
		btnMap.put("UP", btn);
		return btn;
	}

	/**
	 * 下移
	 * @return
	 */
	private JButton createNextBtn(){
		JButton btn = new JButton("下移");
		btn.setIcon(new ImageIcon(ImageConstant.DOWN));
		btn.setToolTipText("下移");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}
				ActionUtils.DownNode();
			}
		});
		btnMap.put("DOWN", btn);
		return btn;
	}

	/**
	 * 提交建模
	 * @return
	 */
	private JButton createSubmitBtn(){
		JButton btn = new JButton("提交");
		btn.setIcon(new ImageIcon(ImageConstant.SUBMIT));
		btn.setToolTipText("提交");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ActionUtils.submitServerDB();
			}
		});
		btnMap.put("SUBMIT", btn);
		return btn;
	}

	/**
	 * 设置
	 * @return
	 */
	private JButton createSetBtn(){
		JButton btn = new JButton("设置");
		btn.setIcon(new ImageIcon(ImageConstant.SETTING));
		btn.setToolTipText("设置");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				/*if(TreeModelPanel.newInstance().getTree().getSelectionCount()<=0){
					DesignUtils.showMessage(MainFrame.getMainFrame(), "请选择需要操作的节点",DesignConstant.PROMIT_TITLE, JOptionPane.ERROR_MESSAGE);
					return;
				}*/
				//ActionUtils.initServerDB();

			}
		});
		btnMap.put("SET", btn);
		return btn;
	}

	/**
	 * 创建帮助按钮
	 * @return
	 */
	private JButton createHelpBtn(){
		JButton btn = new JButton("帮助");
		btn.setIcon(new ImageIcon(ImageConstant.HELP));
		btn.setToolTipText("帮助");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				HelpDialog.newInstance().setVisible(true);
			}
		});
		btnMap.put("HELP", btn);
		return btn;
	}

	/**
	 * 工具栏按钮全部设置不可用
	 */
	private void setBtnDisabled(){
		for (Map.Entry<String, JButton> entry : btnMap.entrySet()) {
			JButton btn = entry.getValue();
			btn.setEnabled(false);
		}
	}

	/**
	 * 设置可用工具栏按钮
	 * @param selectNode当前选中的节点
	 */
	public static void setBtnEnable(DefaultMutableTreeNode selectNode){
		List<String> btnList = new ArrayList<String>();
		BlkTableMetaData selModel = (BlkTableMetaData)selectNode.getUserObject();
        switch(selModel.getModel_type()){
	        case  SYSTEM:{
	        	btnList.add("TABLE");
	        	btnList.add("VIEW");
	        	btnList.add("GROUPS");
	    		break;
	        }
	        case SERVICE:{
	        	btnList.add("TABLE");
	        	btnList.add("VIEW");
	        	btnList.add("GROUPS");
	        	btnList.add("DELETE");
	        	break;
	        }
	        case GROUPS:{
	        	btnList.add("TABLE");
	        	btnList.add("VIEW");
	        	btnList.add("GROUPS");
	        	btnList.add("DELETE");
	        	break;
	        }
	        case TABLE:{
	        	btnList.add("ATTR");
	        	btnList.add("DELETE");
	        	btnList.add("COPY");
	        	btnList.add("PASTE");
	        	break;
	        }
	        case VIEW:{
	        	btnList.add("ATTR");
	        	btnList.add("DELETE");
	        	btnList.add("COPY");
	        	btnList.add("PASTE");
	        	break;
	        }
	        case ATTRIBUTE:{
	        	btnList.add("DELETE");
	        	btnList.add("COPY");
	        	btnList.add("PASTE");
	        	btnList.add("EDIT");
	        	break;
	        }
	        case ROOT:{
	        	btnList.add("SYSTEM");
	        	btnList.add("SERVICE");
	        	break;
	        }
			default:
				break;
	    }
        btnList.add("UP");
        btnList.add("DOWN");
        btnList.add("SUBMIT");
        btnList.add("SET");
        btnList.add("HELP");

		ToolBarMenu.newInstance().setBtnDisabled();
		for(String btnname: btnList){
			if(btnMap.containsKey(btnname)){
				JButton btn = btnMap.get(btnname);
				btn.setEnabled(true);
			}
		}
	}
}
